<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>DSL-es搜索引擎的常见语法 | CANCAN Love Java</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="https://img.cancan.fit/vuepressBlog/avatar.jpg">
    <script language="javascript" type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script language="javascript" type="text/javascript" src="/js/MouseClickEffect.js"></script>
    <script>
          var _hmt = _hmt || [];
          (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?b0aae218897fa9d8a9f76e9a77e0b3c6";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
          })();
        </script>
    <meta name="description" content="这里是灿灿的博客(学习版)...欢迎您的来访">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.c492337a.css" as="style"><link rel="preload" href="/assets/js/app.15f3d1fd.js" as="script"><link rel="preload" href="/assets/js/5.ca401195.js" as="script"><link rel="preload" href="/assets/js/1.8b416e81.js" as="script"><link rel="preload" href="/assets/js/20.d404083b.js" as="script"><link rel="preload" href="/assets/js/12.00b40901.js" as="script"><link rel="prefetch" href="/assets/js/10.a12826ac.js"><link rel="prefetch" href="/assets/js/11.1b7d6dd3.js"><link rel="prefetch" href="/assets/js/13.3a9b9c99.js"><link rel="prefetch" href="/assets/js/14.7d1e871c.js"><link rel="prefetch" href="/assets/js/15.e734b00e.js"><link rel="prefetch" href="/assets/js/16.6eb4ec21.js"><link rel="prefetch" href="/assets/js/17.7474baf0.js"><link rel="prefetch" href="/assets/js/18.ac8965aa.js"><link rel="prefetch" href="/assets/js/19.ebcb0e51.js"><link rel="prefetch" href="/assets/js/21.6f009612.js"><link rel="prefetch" href="/assets/js/22.c65ba1bb.js"><link rel="prefetch" href="/assets/js/23.7fff4e5c.js"><link rel="prefetch" href="/assets/js/24.5e0f2c4a.js"><link rel="prefetch" href="/assets/js/25.f9521823.js"><link rel="prefetch" href="/assets/js/26.7ef32412.js"><link rel="prefetch" href="/assets/js/27.db55beba.js"><link rel="prefetch" href="/assets/js/28.fed9a0d9.js"><link rel="prefetch" href="/assets/js/29.71b5f381.js"><link rel="prefetch" href="/assets/js/3.ece670e3.js"><link rel="prefetch" href="/assets/js/30.353cf836.js"><link rel="prefetch" href="/assets/js/31.461dbf5d.js"><link rel="prefetch" href="/assets/js/4.cfb38e50.js"><link rel="prefetch" href="/assets/js/6.595ebcbc.js"><link rel="prefetch" href="/assets/js/7.bec14422.js"><link rel="prefetch" href="/assets/js/8.8f66a46e.js"><link rel="prefetch" href="/assets/js/9.f4c927b6.js">
    <link rel="stylesheet" href="/assets/css/0.styles.c492337a.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-106e3130><div data-v-106e3130><LoadingPage class="loading-wrapper" data-v-106e3130 data-v-106e3130></LoadingPage> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-4d3be7b7 data-v-106e3130 data-v-106e3130><h3 class="title" style="display:none;" data-v-4d3be7b7 data-v-4d3be7b7>CANCAN Love Java</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-4d3be7b7 data-v-4d3be7b7><input type="password" value="" data-v-4d3be7b7> <span data-v-4d3be7b7>Konck! Knock!</span> <button data-v-4d3be7b7>OK</button></label> <div class="footer" style="display:none;" data-v-4d3be7b7 data-v-4d3be7b7><span data-v-4d3be7b7><i class="iconfont reco-theme" data-v-4d3be7b7></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4d3be7b7>vuePress-theme-reco</a></span> <span data-v-4d3be7b7><i class="iconfont reco-copyright" data-v-4d3be7b7></i> <a data-v-4d3be7b7><span data-v-4d3be7b7>灿灿爱吃肉</span>
            
          <!---->
          2023
        </a></span></div></div> <div class="hide" data-v-106e3130><div data-v-106e3130><div id="smart" class="wrapper-page" style="background-image:url(https://img.cancan.fit/vuepressBlog/8.jpg);background-position-x:center;background-position-y:center;background-size:cover;background-repeat-x:no-repeat;background-repeat-y:no-repeat;" data-v-106e3130><header class="navbar" data-v-106e3130><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="https://img.cancan.fit/vuepressBlog/bear.jpg" alt="CANCAN Love Java" class="logo"> <span class="site-name">CANCAN Love Java</span></a> <div class="links"><div id="dayNightSwitch" class="generalWrapper" data-v-68728e36><a class="click" data-v-68728e36><div class="onOff daySwitch" data-v-68728e36><div class="star star1" data-v-68728e36></div> <div class="star star2" data-v-68728e36></div> <div class="star star3" data-v-68728e36></div> <div class="star star4" data-v-68728e36></div> <div class="star star5" data-v-68728e36></div> <div class="star sky" data-v-68728e36></div> <div class="sunMoon" data-v-68728e36><div class="crater crater1" data-v-68728e36></div> <div class="crater crater2" data-v-68728e36></div> <div class="crater crater3" data-v-68728e36></div> <div class="cloud part1" data-v-68728e36></div> <div class="cloud part2" data-v-68728e36></div></div></div></a></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/日常随笔/" class="nav-link"><i class="iconfont undefined"></i>
  日常随笔
</a></li><li class="dropdown-item"><!----> <a href="/categories/灿灿爱Java/" class="nav-link"><i class="iconfont undefined"></i>
  灿灿爱Java
</a></li><li class="dropdown-item"><!----> <a href="/categories/Linux/" class="nav-link"><i class="iconfont undefined"></i>
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端技术/" class="nav-link"><i class="iconfont undefined"></i>
  前端技术
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/about/" class="nav-link"><i class="iconfont reco-account"></i>
  关于
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-106e3130></div> <aside class="sidebar" data-v-106e3130><div class="personal-info-wrapper" data-v-d528efe2 data-v-106e3130><img src="https://img.cancan.fit/vuepressBlog/avatar.jpg" alt="author-avatar" class="personal-img" data-v-d528efe2> <h3 class="name" data-v-d528efe2>
    灿灿爱吃肉
  </h3> <div class="num" data-v-d528efe2><div data-v-d528efe2><h3 data-v-d528efe2>16</h3> <h6 data-v-d528efe2>文章</h6></div> <div data-v-d528efe2><h3 data-v-d528efe2>17</h3> <h6 data-v-d528efe2>标签</h6></div></div> <hr data-v-d528efe2></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/日常随笔/" class="nav-link"><i class="iconfont undefined"></i>
  日常随笔
</a></li><li class="dropdown-item"><!----> <a href="/categories/灿灿爱Java/" class="nav-link"><i class="iconfont undefined"></i>
  灿灿爱Java
</a></li><li class="dropdown-item"><!----> <a href="/categories/Linux/" class="nav-link"><i class="iconfont undefined"></i>
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端技术/" class="nav-link"><i class="iconfont undefined"></i>
  前端技术
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/about/" class="nav-link"><i class="iconfont reco-account"></i>
  关于
</a></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-4d3be7b7 data-v-106e3130><h3 class="title" style="display:none;" data-v-4d3be7b7 data-v-4d3be7b7>DSL-es搜索引擎的常见语法</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-4d3be7b7 data-v-4d3be7b7><input type="password" value="" data-v-4d3be7b7> <span data-v-4d3be7b7>Konck! Knock!</span> <button data-v-4d3be7b7>OK</button></label> <div class="footer" style="display:none;" data-v-4d3be7b7 data-v-4d3be7b7><span data-v-4d3be7b7><i class="iconfont reco-theme" data-v-4d3be7b7></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4d3be7b7>vuePress-theme-reco</a></span> <span data-v-4d3be7b7><i class="iconfont reco-copyright" data-v-4d3be7b7></i> <a data-v-4d3be7b7><span data-v-4d3be7b7>灿灿爱吃肉</span>
            
          <!---->
          2023
        </a></span></div></div></div> <div data-v-106e3130><main class="page"><div class="page-title" style="display:none;"><h1 class="title">DSL-es搜索引擎的常见语法</h1> <div class="page-info" data-v-04a855f8><i class="iconfont reco-account" data-v-04a855f8><span data-v-04a855f8>灿灿爱吃肉</span></i> <i class="iconfont reco-date" data-v-04a855f8><span data-v-04a855f8>2023-05-01</span></i> <i class="iconfont reco-eye" data-v-04a855f8><span id="/blogs/%E8%8B%A6%E9%80%BC%E7%A8%8B%E5%BA%8F%E5%91%98/JAVA/DSL-es%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E%E7%9A%84%E5%B8%B8%E8%A7%81%E8%AF%AD%E6%B3%95.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-04a855f8><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-04a855f8><span class="tag-item" data-v-04a855f8>Elasticsearch</span><span class="tag-item" data-v-04a855f8>es</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><p></p><div class="table-of-contents"><ul><li><a href="#_1-新增索引库">1.新增索引库</a></li><li><a href="#_2-查看索引-删除索引">2.查看索引,删除索引</a></li><li><a href="#_3-修改索引库-仅能新增-不能真的修改-如需修改-可删除后再创建">3.修改索引库(仅能新增,不能真的修改,如需修改,可删除后再创建)</a></li><li><a href="#_4-新增文档">4.新增文档</a></li><li><a href="#_5-根据id查询文档-根据id删除文档">5.根据ID查询文档,根据ID删除文档</a></li><li><a href="#_6-全量修改文档-先删除-再新增">6.全量修改文档=&gt;先删除,再新增</a></li><li><a href="#_7-局部修改文档字段-不存在则新增">7.局部修改文档字段(不存在则新增)</a></li><li><a href="#_8-例子-新增酒店索引">8.例子: 新增酒店索引</a></li><li><a href="#_9-dsl查询语法">9.DSL查询语法</a><ul><li><a href="#_9-1-dsl查询分类和基本语法">9.1.DSL查询分类和基本语法</a></li><li><a href="#_9-2-全文检索查询-会对用户输入的内容分词-常用于搜索框搜索">9.2.全文检索查询(会对用户输入的内容分词,常用于搜索框搜索)</a></li><li><a href="#_9-3-精确查询-一般是查找keyword、数值、日期、boolean等类型字段。所以不会对搜索条件分词">9.3.精确查询(一般是查找keyword、数值、日期、boolean等类型字段。所以不会对搜索条件分词)</a></li><li><a href="#_9-4-地理查询-根据经纬度查询">9.4.地理查询,根据经纬度查询</a></li><li><a href="#_9-5-复合查询">9.5.复合查询</a><ul><li><a href="#_9-5-1-修改文档的相关性算分">9.5.1.修改文档的相关性算分</a></li><li><a href="#_9-5-2-boolean-query-布尔查询是一个或多个查询子句的组合。">9.5.2.Boolean Query(布尔查询是一个或多个查询子句的组合。)</a></li></ul></li></ul></li><li><a href="#_10-排序">10.排序</a></li><li><a href="#_11-分页">11.分页</a></li><li><a href="#_12-高亮">12.高亮</a></li><li><a href="#_13-搜索结果处理整体语法">13.搜索结果处理整体语法</a></li><li><a href="#_14-数据聚合-dsl实现-桶-bucket-聚合">14.数据聚合-DSL实现 桶(Bucket)聚合</a><ul><li><a href="#_14-1-自定义">14.1.自定义</a></li><li><a href="#_14-2-自定义顺序规则-默认降序">14.2.自定义顺序规则(默认降序)</a></li><li><a href="#_14-2-限定聚合范围">14.2.限定聚合范围</a></li></ul></li><li><a href="#_15-数据聚合-dsl实现-度量-metrics-聚合">15.数据聚合-DSL实现 度量(Metrics)聚合</a></li><li><a href="#_16-自定义分词器">16.自定义分词器!!!!!!</a></li><li><a href="#_17-自动补全索引库">17.自动补全索引库</a></li><li><a href="#_18-例子-新增酒店索引">18.例子:新增酒店索引</a></li></ul></div><p></p> <h2 id="_1-新增索引库"><a href="#_1-新增索引库" class="header-anchor">#</a> 1.新增索引库</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>PUT /323test
{
  &quot;mappings&quot;: {
    &quot;properties&quot;: {
      &quot;info&quot;: {
        &quot;type&quot;: &quot;text&quot;,
        &quot;analyzer&quot;: &quot;ik_smart&quot;
      },
      &quot;email&quot;: {
        &quot;type&quot;: &quot;keyword&quot;,
        &quot;index&quot;: false
      },
      &quot;name&quot;: {
        &quot;properties&quot;: {
          &quot;firstName&quot;: {
            &quot;type&quot;: &quot;keyword&quot;
          },
           &quot;lastName&quot;: {
            &quot;type&quot;: &quot;keyword&quot;
          }
        }
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h2 id="_2-查看索引-删除索引"><a href="#_2-查看索引-删除索引" class="header-anchor">#</a> 2.查看索引,删除索引</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /323test

DELETE /323test
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="_3-修改索引库-仅能新增-不能真的修改-如需修改-可删除后再创建"><a href="#_3-修改索引库-仅能新增-不能真的修改-如需修改-可删除后再创建" class="header-anchor">#</a> 3.修改索引库(仅能新增,不能真的修改,如需修改,可删除后再创建)</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>PUT /323test/_mapping
{
  &quot;properties&quot;: {
    &quot;age&quot;: {
      &quot;type&quot;: &quot;integer&quot;
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="_4-新增文档"><a href="#_4-新增文档" class="header-anchor">#</a> 4.新增文档</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>POST /323test/_doc/1
{
  &quot;age&quot;: 24,
  &quot;email&quot;: &quot;717166157@qq.com&quot;,
  &quot;info&quot;: &quot;啊哈哈哈,开心开心,我爱es搜索引擎,我爱java,我爱喝咖啡&quot;,
  &quot;name&quot;: {
    &quot;firstName&quot;: &quot;林灿&quot;,
    &quot;lastName&quot;: &quot;何&quot;
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h2 id="_5-根据id查询文档-根据id删除文档"><a href="#_5-根据id查询文档-根据id删除文档" class="header-anchor">#</a> 5.根据ID查询文档,根据ID删除文档</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /323test/_doc/1
DELETE /323test/_doc/1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="_6-全量修改文档-先删除-再新增"><a href="#_6-全量修改文档-先删除-再新增" class="header-anchor">#</a> 6.全量修改文档=&gt;先删除,再新增</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>PUT /323test/_doc/1
{
  &quot;age&quot;: 24,
  &quot;email&quot;: &quot;717166157@163.com&quot;,
  &quot;info&quot;: &quot;啊哈哈哈,开心开心,我爱es搜索引擎,我爱java,我爱喝咖啡&quot;,
  &quot;name&quot;: {
    &quot;firstName&quot;: &quot;林灿&quot;,
    &quot;lastName&quot;: &quot;何&quot;
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h2 id="_7-局部修改文档字段-不存在则新增"><a href="#_7-局部修改文档字段-不存在则新增" class="header-anchor">#</a> 7.局部修改文档字段(不存在则新增)</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>POST /323test/_update/1
{
  &quot;doc&quot;: {
    &quot;email&quot;: &quot;717166157@qq.com&quot;
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="_8-例子-新增酒店索引"><a href="#_8-例子-新增酒店索引" class="header-anchor">#</a> 8.例子: 新增酒店索引</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code># 新增酒店的mapping(索引)
PUT /hotel
{
  &quot;mappings&quot;: {
    &quot;properties&quot;: {
      &quot;id&quot;: {
        &quot;type&quot;: &quot;keyword&quot;
      },
      &quot;name&quot;: {
        &quot;type&quot;: &quot;text&quot;,
        &quot;analyzer&quot;: &quot;ik_max_word&quot;,
        &quot;copy_to&quot;: &quot;all&quot;
      },
      &quot;address&quot;: {
        &quot;type&quot;: &quot;keyword&quot;,
        &quot;index&quot;: false
      },
      &quot;price&quot;: {
        &quot;type&quot;: &quot;integer&quot;
      },
      &quot;score&quot;: {
        &quot;type&quot;: &quot;integer&quot;
      },
      &quot;brand&quot;: {
        &quot;type&quot;: &quot;keyword&quot;,
        &quot;copy_to&quot;: &quot;all&quot;
      },
      &quot;city&quot;: {
        &quot;type&quot;: &quot;keyword&quot;
      },
      &quot;starName&quot;: {
        &quot;type&quot;: &quot;keyword&quot;
      },
      &quot;business&quot;: {
        &quot;type&quot;: &quot;keyword&quot;,
        &quot;copy_to&quot;: &quot;all&quot;
      },
      &quot;location&quot;: {
        &quot;type&quot;: &quot;geo_point&quot;
      },
      &quot;pic&quot;: {
        &quot;type&quot;: &quot;keyword&quot;,
        &quot;index&quot;: false
      },
      &quot;all&quot;: {
        &quot;type&quot;: &quot;text&quot;,
        &quot;analyzer&quot;: &quot;ik_max_word&quot;
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br></div></div><h2 id="_9-dsl查询语法"><a href="#_9-dsl查询语法" class="header-anchor">#</a> 9.DSL查询语法</h2> <h3 id="_9-1-dsl查询分类和基本语法"><a href="#_9-1-dsl查询分类和基本语法" class="header-anchor">#</a> 9.1.DSL查询分类和基本语法</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code># 查看所有文档
GET /hotel/_doc/_search

# 查询所有文档
GET /hotel/_doc/_search
{
  &quot;query&quot;: {
    &quot;match_all&quot;: {}
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="_9-2-全文检索查询-会对用户输入的内容分词-常用于搜索框搜索"><a href="#_9-2-全文检索查询-会对用户输入的内容分词-常用于搜索框搜索" class="header-anchor">#</a> 9.2.全文检索查询(会对用户输入的内容分词,常用于搜索框搜索)</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code># match查询(推荐)!!!
GET /hotel/_doc/_search
{
  &quot;query&quot;: {
    &quot;match&quot;: {
      &quot;all&quot;:&quot;外滩 如家&quot;
    }
  }
}

# multi_match查询(多字段查询,效率低)
GET /hotel/_doc/_search
{
  &quot;query&quot;: {
    &quot;multi_match&quot;: {
      &quot;query&quot;:&quot;外滩如家&quot;,
      &quot;fields&quot;:[&quot;brand&quot;,&quot;business&quot;,&quot;name&quot;]
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="_9-3-精确查询-一般是查找keyword、数值、日期、boolean等类型字段。所以不会对搜索条件分词"><a href="#_9-3-精确查询-一般是查找keyword、数值、日期、boolean等类型字段。所以不会对搜索条件分词" class="header-anchor">#</a> 9.3.精确查询(一般是查找keyword、数值、日期、boolean等类型字段。所以不会对搜索条件分词)</h3> <ul><li>term:根据词条精确值查询</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code># term查询
GET /hotel/_doc/_search
{
  &quot;query&quot;: {
    &quot;term&quot;: {
      &quot;city&quot;:{
        &quot;value&quot;:&quot;上海&quot;
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><ul><li>range:根据值的范围查询</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code># range查询
GET /hotel/_doc/_search
{
  &quot;query&quot;: {
    &quot;range&quot;: {
      &quot;price&quot;:{
        &quot;gte&quot;:100,
        &quot;lte&quot;:200
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="_9-4-地理查询-根据经纬度查询"><a href="#_9-4-地理查询-根据经纬度查询" class="header-anchor">#</a> 9.4.地理查询,根据经纬度查询</h3> <ul><li>geo_distance:查询到指定中心点小于某个距离值的所有文档</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_doc/_search
{
  &quot;query&quot;: {
    &quot;geo_distance&quot;: {
      &quot;distance&quot;: &quot;15km&quot;,
      &quot;location&quot;: &quot;31.21,121.5&quot;
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_9-5-复合查询"><a href="#_9-5-复合查询" class="header-anchor">#</a> 9.5.复合查询</h3> <h4 id="_9-5-1-修改文档的相关性算分"><a href="#_9-5-1-修改文档的相关性算分" class="header-anchor">#</a> 9.5.1.修改文档的相关性算分</h4> <ul><li>使用function score query，可以修改文档的相关性算分(query score)，根据新得到的算分排序。</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_search
{
  &quot;query&quot;: {
    &quot;function_score&quot;: {
      &quot;query&quot;: {
        &quot;match&quot;: {
          &quot;all&quot;: &quot;外滩&quot;
        }
      },
      &quot;functions&quot;: [
        {
          &quot;filter&quot;: {
            &quot;term&quot;: {
              &quot;brand&quot;: &quot;如家&quot;
            }
          },
          &quot;weight&quot;: 10
        }
      ],
      &quot;boost_mode&quot;: &quot;sum&quot;
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h4 id="_9-5-2-boolean-query-布尔查询是一个或多个查询子句的组合。"><a href="#_9-5-2-boolean-query-布尔查询是一个或多个查询子句的组合。" class="header-anchor">#</a> 9.5.2.Boolean Query(布尔查询是一个或多个查询子句的组合。)</h4> <p>子查询的组合方式有:</p> <ul><li><p>must:必须匹配每个子查询，类似“与”</p></li> <li><p>should:选择性匹配子查询，类似“或”</p></li> <li><p>must_not:必须不匹配，不参与算分，类似“非”</p></li> <li><p>filter:必须匹配，不参与算分</p></li></ul> <p>需求:搜索名字包含“如家”，价格不高于400，在坐标31.21,121.5周围10km范围内的酒店。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>利用bool查询实现功能
GET /hotel/_search
{
  &quot;query&quot;: {
    &quot;bool&quot;: {
      &quot;must&quot;: [
        {
          &quot;match&quot;: {
            &quot;name&quot;: &quot;如家&quot;
          }
        }
      ],
      &quot;must_not&quot;: [
        {
          &quot;range&quot;: {
            &quot;price&quot;: {
              &quot;gt&quot;: 400
            }
          }
        }
      ],
      &quot;filter&quot;: [
        {
          &quot;geo_distance&quot;: {
            &quot;distance&quot;: &quot;10km&quot;,
            &quot;location&quot;: {
              &quot;lat&quot;: 31.21,
              &quot;lon&quot;: 121.5
            }
          }
        }
      ]
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h2 id="_10-排序"><a href="#_10-排序" class="header-anchor">#</a> 10.排序</h2> <p>elasticsearch支持对搜索结果排序，默认是根据相关度算分(_score)来排序。可以排序字段类型有: keyword类型、数值类型、地理坐标类型、日期类型等。</p> <p>案例:对酒店数据按照用户评价降序排序，评价相同的按照价格升序排序</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_search
{
  &quot;query&quot;: {
    &quot;match_all&quot;: {}
  },
  &quot;sort&quot;: [
    {
      &quot;score&quot;: &quot;desc&quot;,
      &quot;price&quot;: &quot;asc&quot;
    }
  ]
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>案例:实现对酒店数据按照到你的位置坐标的距离升序排序</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_search
{
  &quot;query&quot;: {
    &quot;match_all&quot;: {}
  },
  &quot;sort&quot;: [
    {
      &quot;_geo_distance&quot;: {
        &quot;location&quot;: {
          &quot;lat&quot;: 31.034661,
          &quot;lon&quot;:  121.612282
        },
        &quot;order&quot;: &quot;asc&quot;,
        &quot;unit&quot;: &quot;km&quot;
      }
    }
  ]
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h2 id="_11-分页"><a href="#_11-分页" class="header-anchor">#</a> 11.分页</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_search
{
  &quot;query&quot;: {
    &quot;match_all&quot;: {}
  },
  &quot;sort&quot;: [
    {
      &quot;price&quot;: &quot;asc&quot;
    }
  ],
  &quot;from&quot;: 30,
  &quot;size&quot;: 20
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h2 id="_12-高亮"><a href="#_12-高亮" class="header-anchor">#</a> 12.高亮</h2> <p>高亮查询，默认情况下，ES搜索字段必须与高亮字段一致,所以需要设置require_field_match为false</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_search
{
  &quot;query&quot;: {
    &quot;match&quot;: {
      &quot;all&quot;: &quot;如家&quot;
    }
  },
  &quot;highlight&quot;: {
    &quot;fields&quot;: {
      &quot;name&quot;: {
        &quot;require_field_match&quot;: &quot;false&quot;,
        &quot;pre_tags&quot;: &quot;&lt;em&gt;&quot;,
        &quot;post_tags&quot;: &quot;&lt;em&gt;&quot;
      },
      &quot;brand&quot;: {
        &quot;require_field_match&quot;: &quot;false&quot;,
        &quot;pre_tags&quot;: &quot;&lt;em&gt;&quot;,
        &quot;post_tags&quot;: &quot;&lt;em&gt;&quot;
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h2 id="_13-搜索结果处理整体语法"><a href="#_13-搜索结果处理整体语法" class="header-anchor">#</a> 13.搜索结果处理整体语法</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_search
{
  &quot;query&quot;: {
    &quot;match&quot;: {
      &quot;all&quot;: &quot;如家&quot;
    }
  },
  &quot;from&quot;: 0,
  &quot;size&quot;: 20,
  &quot;sort&quot;: [
    {
      &quot;price&quot;: &quot;asc&quot;
    },
    {
      &quot;_geo_distance&quot;: {
        &quot;location&quot;: {
          &quot;lat&quot;: 31.034661,
          &quot;lon&quot;: 121.612282
        },
        &quot;order&quot;: &quot;asc&quot;,
        &quot;unit&quot;: &quot;km&quot;
      }
    }
  ],
  &quot;highlight&quot;: {
    &quot;fields&quot;: {
      &quot;name&quot;: {
        &quot;require_field_match&quot;: &quot;false&quot;,
        &quot;pre_tags&quot;: &quot;&lt;em&gt;&quot;,
        &quot;post_tags&quot;: &quot;&lt;em&gt;&quot;
      },
      &quot;brand&quot;: {
        &quot;require_field_match&quot;: &quot;false&quot;,
        &quot;pre_tags&quot;: &quot;&lt;em&gt;&quot;,
        &quot;post_tags&quot;: &quot;&lt;em&gt;&quot;
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><h2 id="_14-数据聚合-dsl实现-桶-bucket-聚合"><a href="#_14-数据聚合-dsl实现-桶-bucket-聚合" class="header-anchor">#</a> 14.数据聚合-DSL实现 桶(Bucket)聚合</h2> <h3 id="_14-1-自定义"><a href="#_14-1-自定义" class="header-anchor">#</a> 14.1.自定义</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_search
{
  &quot;size&quot;: 0,
  &quot;aggs&quot;: {
    &quot;brandAgg&quot;: {
      &quot;terms&quot;: {
        &quot;field&quot;: &quot;brand&quot;,
        &quot;size&quot;: 10
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="_14-2-自定义顺序规则-默认降序"><a href="#_14-2-自定义顺序规则-默认降序" class="header-anchor">#</a> 14.2.自定义顺序规则(默认降序)</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_search
{
  &quot;size&quot;: 0,
  &quot;aggs&quot;: {
    &quot;brandAgg&quot;: {
      &quot;terms&quot;: {
        &quot;field&quot;: &quot;brand&quot;,
        &quot;size&quot;: 10,
        &quot;order&quot;: {
          &quot;_count&quot;: &quot;asc&quot;
        }
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="_14-2-限定聚合范围"><a href="#_14-2-限定聚合范围" class="header-anchor">#</a> 14.2.限定聚合范围</h3> <p>默认情况下，Bucket聚合是对索引库的所有文档做聚合，如果上亿条数据这种情况,会极大消耗内存, 所以我们可以限定要聚合的文档范围，只要添加query条件即可;</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_search
{
  &quot;query&quot;: {
    &quot;range&quot;: {
      &quot;price&quot;: {
        &quot;gte&quot;: 100,
        &quot;lte&quot;: 300
      }
    }
  }, 
  &quot;size&quot;: 0,
  &quot;aggs&quot;: {
    &quot;brandAgg&quot;: {
      &quot;terms&quot;: {
        &quot;field&quot;: &quot;brand&quot;,
        &quot;size&quot;: 10
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h2 id="_15-数据聚合-dsl实现-度量-metrics-聚合"><a href="#_15-数据聚合-dsl实现-度量-metrics-聚合" class="header-anchor">#</a> 15.数据聚合-DSL实现 度量(Metrics)聚合</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>#嵌套聚合metric
GET /hotel/_search
{
  &quot;size&quot;: 0,
  &quot;aggs&quot;: {
    &quot;brandAgg&quot;: {
      &quot;terms&quot;: {
        &quot;field&quot;: &quot;brand&quot;,
        &quot;size&quot;: 10
      },
      &quot;aggs&quot;: {
        &quot;scroeAgg&quot;: {
          &quot;stats&quot;: {
            &quot;field&quot;: &quot;score&quot;
          }
        }
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p>实例:根据不同品牌的酒店的平均分进行降序排序</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /hotel/_search
{
  &quot;size&quot;: 0,
  &quot;aggs&quot;: {
    &quot;brandAgg&quot;: {
      &quot;terms&quot;: {
        &quot;field&quot;: &quot;brand&quot;,
        &quot;size&quot;: 10,
        &quot;order&quot;: {
          &quot;scroeAgg.avg&quot;: &quot;desc&quot;
        }
      },
      &quot;aggs&quot;: {
        &quot;scroeAgg&quot;: {
          &quot;stats&quot;: {
            &quot;field&quot;: &quot;score&quot;
          }
        }
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h2 id="_16-自定义分词器"><a href="#_16-自定义分词器" class="header-anchor">#</a> 16.自定义分词器!!!!!!</h2> <p><img src="https://img-blog.csdnimg.cn/img_convert/c73cf0af539a4790b93925460bf4964a.png" alt=""></p> <p>可以在创建索引库时，通过settings来配置自定义的analyzer(分词器)︰</p> <ul><li><p>拼音分词器适合在创建倒排索引的时候使用，但不能在搜索的时候使用。搜索时使用ik_smart或者其他分词器,因为当搜索时也用自定义分词器的话,用户搜索狮子时,就会把虱子搜索出来</p></li> <li><p>&quot;analyzer&quot;:&quot;my_analyzer&quot; // 创建时使用自定义分词器</p></li> <li><p>&quot;search_analyzer&quot;:&quot;ik_smart&quot; // 搜索时使用ik_smart分词器</p></li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>// 此自定义分词器仅存在于本索引库中
PUT /test
{
    &quot;settings&quot;: {
        &quot;analysis&quot;: {
            &quot;analyzer&quot;: {
                &quot;my_analyzer&quot;: {
                    &quot;tokenizer&quot;: &quot;ik_max_word&quot;,
                    &quot;filter&quot;: &quot;py&quot;
                }
            },
            &quot;filter&quot;: {
                &quot;py&quot;: {
                    &quot;type&quot;: &quot;pinyin&quot;,
                    &quot;keep_full_pinyin&quot;: false,
                    &quot;keep_joined_full_pinyin&quot;: true,
                    &quot;keep_original&quot;: true,
                    &quot;limit_first_letter_length&quot;: 16,
                    &quot;remove_duplicated_term&quot;: true,
                    &quot;none_chinese_pinyin_tokenize&quot;: false
                }
            }
        }
    },
    &quot;mappings&quot;:{
        &quot;properties&quot;:{
            &quot;name&quot;:{
                &quot;type&quot;:&quot;text&quot;,
                // 创建时使用自定义分词器
                &quot;analyzer&quot;:&quot;my_analyzer&quot;,
                // 搜索时使用ik_smart分词器(如果搜索时也用自定义分词器,用户搜索狮子时,也会把虱子搜索出来)
                &quot;search_analyzer&quot;:&quot;ik_smart&quot;
            }
        }
    }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div><h2 id="_17-自动补全索引库"><a href="#_17-自动补全索引库" class="header-anchor">#</a> 17.自动补全索引库</h2> <div class="language- line-numbers-mode"><pre class="language-text"><code>// 创建自动补全索引库
PUT test2
{
  &quot;mappings&quot;: {
    &quot;properties&quot;: {
      &quot;title&quot;:{
        &quot;type&quot;: &quot;completion&quot;
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><div class="language- line-numbers-mode"><pre class="language-text"><code>// 自动补全查询
POST /test2/_search
{
  &quot;suggest&quot;: {
    &quot;title_suggest&quot;: {
      &quot;text&quot;: &quot;s&quot;, // 关键字
      &quot;completion&quot;: {
        &quot;field&quot;: &quot;title&quot;, // 补全字段
        &quot;skip_duplicates&quot;: true, // 跳过重复的
        &quot;size&quot;: 10 // 获取前10条结果
      }
    }
  }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="_18-例子-新增酒店索引"><a href="#_18-例子-新增酒店索引" class="header-anchor">#</a> 18.例子:新增酒店索引</h2> <p>再copy一遍</p> <p><img src="https://img-blog.csdnimg.cn/img_convert/c73cf0af539a4790b93925460bf4964a.png" alt=""></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>// 酒店数据索引库
PUT /hotel
{
  &quot;settings&quot;: {
    &quot;analysis&quot;: {
      &quot;analyzer&quot;: {
        &quot;text_anlyzer&quot;: {
          &quot;tokenizer&quot;: &quot;ik_max_word&quot;,
          &quot;filter&quot;: &quot;py&quot;
        },
        &quot;completion_analyzer&quot;: {
          &quot;tokenizer&quot;: &quot;keyword&quot;,
          &quot;filter&quot;: &quot;py&quot;
        }
      },
      &quot;filter&quot;: {
        &quot;py&quot;: {
          &quot;type&quot;: &quot;pinyin&quot;,
          &quot;keep_full_pinyin&quot;: false,
          &quot;keep_joined_full_pinyin&quot;: true,
          &quot;keep_original&quot;: true,
          &quot;limit_first_letter_length&quot;: 16,
          &quot;remove_duplicated_term&quot;: true,
          &quot;none_chinese_pinyin_tokenize&quot;: false
        }
      }
    }
  },
  &quot;mappings&quot;: {
    &quot;properties&quot;: {
      &quot;id&quot;:{
        &quot;type&quot;: &quot;keyword&quot;
      },
      &quot;name&quot;:{
        &quot;type&quot;: &quot;text&quot;,
        &quot;analyzer&quot;: &quot;text_anlyzer&quot;,
        &quot;search_analyzer&quot;: &quot;ik_smart&quot;,
        &quot;copy_to&quot;: &quot;all&quot;
      },
      &quot;address&quot;:{
        &quot;type&quot;: &quot;keyword&quot;,
        &quot;index&quot;: false
      },
      &quot;price&quot;:{
        &quot;type&quot;: &quot;integer&quot;
      },
      &quot;score&quot;:{
        &quot;type&quot;: &quot;integer&quot;
      },
      &quot;brand&quot;:{
        &quot;type&quot;: &quot;keyword&quot;,
        &quot;copy_to&quot;: &quot;all&quot;
      },
      &quot;city&quot;:{
        &quot;type&quot;: &quot;keyword&quot;
      },
      &quot;starName&quot;:{
        &quot;type&quot;: &quot;keyword&quot;
      },
      &quot;business&quot;:{
        &quot;type&quot;: &quot;keyword&quot;,
        &quot;copy_to&quot;: &quot;all&quot;
      },
      &quot;location&quot;:{
        &quot;type&quot;: &quot;geo_point&quot;
      },
      &quot;pic&quot;:{
        &quot;type&quot;: &quot;keyword&quot;,
        &quot;index&quot;: false
      },
      &quot;all&quot;:{
        &quot;type&quot;: &quot;text&quot;,
        &quot;analyzer&quot;: &quot;text_anlyzer&quot;,
        &quot;search_analyzer&quot;: &quot;ik_smart&quot;
      },
      &quot;suggestion&quot;:{
          &quot;type&quot;: &quot;completion&quot;,
          &quot;analyzer&quot;: &quot;completion_analyzer&quot;
      }
    }
  }
}

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br></div></div></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----> <!----> <!----></main> <!----></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="Sakura" data-v-1137e66e><canvas id="canvas_sakura" style="z-index:5;" data-v-1137e66e></canvas></div><div></div><canvas id="vuepress-canvas-cursor"></canvas><APlayer audio="" fixed="true" mini="true" autoplay="autoplay" theme="#b7daff" loop="loop" order="list" preload="auto" volume="0.15" mutex="true" lrc-type="0" list-max-height="250" storage-name="vuepress-plugin-meting" id="aplayer-fixed"></APlayer><!----></div></div>
    <script src="/assets/js/app.15f3d1fd.js" defer></script><script src="/assets/js/5.ca401195.js" defer></script><script src="/assets/js/1.8b416e81.js" defer></script><script src="/assets/js/20.d404083b.js" defer></script><script src="/assets/js/12.00b40901.js" defer></script>
  </body>
</html>
